package com.vue.cn.utils.upload;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.Part;
import java.util.UUID;

/**
 * @description UUID生成文件名+文件目录打散
 * @author pzy
 * @updateTime 2020/10/24 11:31
 */
@Component
public class FileUtils {

    public static String getFileName(Part part) {
        String ext = StringUtils.getFilenameExtension(part.getSubmittedFileName());
        //文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        int hashCode = fileName.hashCode();
        String dir1 = Integer.toHexString(hashCode & 0xff);
        String dir2 = Integer.toHexString((hashCode >> 8) & 0xff);
        return dir1 + "/" + dir2 + "/" + fileName + "." + ext;
    }

    public static String getFileName(String imgName) {
        String ext = imgName.substring(imgName.indexOf(".")+1);
        //文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        int hashCode = fileName.hashCode();
        String dir1 = Integer.toHexString(hashCode & 0xff);
        String dir2 = Integer.toHexString((hashCode >> 8) & 0xff);
        String realPath = dir1 + "/" + dir2 + "/" + fileName + "." + "txt";
        return realPath;
    }


}
